﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebClasses.Pages;
using DomainModel;
using System.IO;

public partial class Admin_ProductCenter_ProductEdit : NormalPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindingCategory();
            InitialPage();
        }
    }

    public int ProductID
    {
        get
        {
            return this.GetParam("ID", true, false, 0);
        }
    }

    static string ImageRelativePath = "Files\\Product\\";

    private void BindingCategory()
    {
        ddlCategory.Items.Clear();

        IEnumerable<Category> allCategories = this.LogicController.ProductManager.GetAllCategories();
        IEnumerable<Category> firstLevelCategory = allCategories.Where<Category>(c => c.ParentID == 1);//hard-coding the root category id
        IEnumerable<Category> secondLevelCategory;
        string showText = string.Empty;

        foreach (Category cate in firstLevelCategory)
        {
            ddlCategory.Items.Add(new ListItem(cate.CategoryName, cate.ID.ToString()));
            secondLevelCategory = allCategories.Where<Category>(c => c.ParentID == cate.ID);
            foreach (Category child in secondLevelCategory)
            {
                showText = "--" + child.CategoryName;
                ddlCategory.Items.Add(new ListItem(showText, child.ID.ToString()));
            }
        }
    }

    private void InitialPage()
    {
        int productId = this.ProductID;
        if (productId == 0) return;

        Product product = this.LogicController.ProductManager.GetProductByID(productId);
        txtDetail.Value = product.Detail;
        txtModel.Text = product.Model;
        txtName.Text = product.ProductName;
        txtPrice.Text = product.Price.ToString();
        txtRank.Text = product.Rank.ToString();
        chbRecommend.Checked = product.Recommend;
        ddlCategory.Items.FindByValue(product.CategoryID.ToString()).Selected = true;
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        decimal price = 0.0m;
        int rank = 0;
        string imagePath = string.Empty;
        decimal.TryParse(txtPrice.Text, out price);
        int.TryParse(txtRank.Text, out rank);
        if (flImage.HasFile)
        {
            string exten = Path.GetExtension(flImage.FileName);
            imagePath = AppDomain.CurrentDomain.BaseDirectory + ImageRelativePath;
            if (!Directory.Exists(imagePath))
            {
                Directory.CreateDirectory(imagePath);
            }

            imagePath += this.ProductID + "_" + DateTime.Now.Ticks.ToString() + exten;

            flImage.SaveAs(imagePath);
        }

        Product product = new Product
        {
            ID = this.ProductID,
            CategoryID = int.Parse(ddlCategory.SelectedValue),
            CreateTime = DateTime.Now,
            Model = txtModel.Text,
            Price = price,
            ProductName = txtName.Text,
            Rank = rank,
            Recommend = chbRecommend.Checked,
            Detail = txtDetail.Value,
        };

        product.ImageUrl = (this.ProductID == 0 || imagePath != string.Empty) ? imagePath :
                                                    this.LogicController.ProductManager.GetProductByID(this.ProductID).ImageUrl;

        this.LogicController.ProductManager.EditProduct(product);
    }
}
